package com.kechen.client.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author 0xHertz
 * @Date: 2022/11/7 0007 14:23
 * @Description:
 **/
@RestController
public class TestRibbonController {

    @Autowired
    private EurekaClient eurekaClient;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/rob")
    public String rob(){
        InstanceInfo info = eurekaClient.getNextServerFromEureka("QF-SEARCH", false);

        //2. 获取到访问的地址
        String url = info.getHomePageUrl();
        System.out.println(url);

        //3. 通过restTemplate访问, 注意这里必须是通过服务名访问, 才可以负载均衡
        String result = restTemplate.getForObject("http://QF-SEARCH/search", String.class);

        //4. 返回
        return result;
    }
}
